<?php
class ControllerSaleHoaHong extends Controller {
	private $error = array();

	public function index() {
	$this->language->load('sale/customer');

		$this->document->setTitle('Lịch sử thanh toán & Chuyển tiền');

		$this->load->model('sale/customer');
		if(isset($this->request->get['customer_id'])){
			$results = $this->model_sale_customer->getCustomer($this->request->get['customer_id']);
			if($results){
			// hoa hồng trực tiếp
			$results1 = $this->model_sale_customer->getAllhhBaotro($results['customer_id']);	
			// hoa hồng gián tiếp
			$results2 = $this->model_sale_customer->gethhTuvan($results['customer_id']);
			$results3 = $this->model_sale_customer->getTotalHoahongTructiep($results['customer_id']);
			$results4 = $this->model_sale_customer->getTotalHoahongGiantiep($results['customer_id']);
			$customers = $this->model_sale_customer->getCustomersByParent($results['customer_id']);
			$transactions = $this->model_sale_customer->getTransactionByCustomer($results['customer_id']);

			$this->data['customers'] = array();
			$this->data['hhtuvan'] = array();
			$this->data['hhbaotro'] = array();
			$this->data['transactions'] = array();

			$this->data['totalbaotro'] = 0;
			$this->data['totalbaotronhan'] = 0;
			$this->data['totalbaotrochuanhan'] = 0;
			
			$this->data['totaltuvan'] = 0;
			$this->data['totaltuvannhan'] = 0;
			$this->data['totaltuvanchuanhan'] = 0;
			
			$this->data['totalnhan'] = 0;
			$this->data['totalchuanhan'] = 0;
			$this->data['total'] = 0;
			$totalbaotronhan = 0;
			$totaltuvannhan = 0;
			$totalbaotrochuanhan = 0;
			$totaltuvanchuanhan = 0;
			// hoa hồng trực tiếp
			if($results1){
				foreach($results1 as $result){				
				  $cus = $this->model_sale_customer->getCustomer($result['customer_id']);
					if($cus){
					if((int)$result['status'] == 1 ){
						$totalbaotronhan+= $result['price'];
					}	
					
					$this->data['hhbaotro'][] = array(
							'date_added' => date('d/m/Y',strtotime($result['date_added'])),
							'date_join' => date('d/m/Y',strtotime($cus['date_added'])),
							'name' => $cus['name'],
							'madn' => $cus['madn'],
							'telephone' => $cus['telephone'],
							'email' => $cus['email'],
							'hoahong' => $this->currency->format($result['price']),
							'date_edit' => date('d/m/Y',strtotime($result['date_edit'])),
							'status' => $result['status']
						);	
				}}
			}	
			$this->data['totalbaotronhan']=	$this->currency->format($totalbaotronhan);
			$this->data['totalbaotro'] = $this->currency->format($results3['total']);	
			$totalbaotrochuanhan = $results3['total'] - $totalbaotronhan;
			$this->data['totalbaotrochuanhan'] = $this->currency->format($totalbaotrochuanhan);
			
			// hoa hồng gián tiếp
			if($results2){
				foreach($results2 as $result){
				$cus = $this->model_sale_customer->getCustomer($result['customer_id']);
					if($cus){
						if((int)$result['status'] == 1 ){
							$totaltuvannhan += $result['price'];
						}	
						$this->data['hhtuvan'][] = array(
								'date_join' => date('d/m/Y',strtotime($cus['date_added'])),
								'date_added' => date('d/m/Y',strtotime($result['date_added'])),
								'name' => $cus['name'],
								'madn' => $cus['madn'],
								'email' => $cus['email'],
								'telephone' => $cus['telephone'],
								'date_edit' => date('d/m/Y',strtotime($result['date_edit'])),
								'status' => $result['status'],
								'hoahong' => $this->currency->format($result['price'])
							);
					}
				}
			}
			// Customer
			if($customers){
				foreach($customers as $customer){
					$this->data['customers'][] = array(
						'name'       => $customer['name'],
						'madn'       => $customer['madn'],
						'email'      => $customer['email'],
						'telephone'  => $customer['telephone'],
						'address'    => $customer['address'],
						'status'  => $customer['status'],
						'date_added' => date('d/m/Y',strtotime($customer['date_added']))
					);
				}
			}
		// Transactions
			if($transactions){
				foreach($transactions as $transaction){
					$this->data['transactions'][] = array(
						'price'      => $this->currency->format($transaction['price']),
						'comment'    => $transaction['comment'],
						'date_added' => date('d/m/Y',strtotime($transaction['date_added']))
					);
				}
			}
		
			$this->data['totaltuvannhan']=	$this->currency->format($totaltuvannhan);
			$totaltuvanchuanhan = $results4['total'] - $totaltuvannhan;
			$this->data['totaltuvan']   = $this->currency->format($results4['total']);
			$this->data['totaltuvanchuanhan'] = $this->currency->format($totaltuvanchuanhan);
			
			$this->data['total'] = $this->currency->format($results4['total']+$results3['total']);
			$this->data['totalchuanhan'] = $this->currency->format($totaltuvanchuanhan+$totalbaotrochuanhan);
			$this->data['totalnhan'] = $this->currency->format($totalbaotronhan+$totaltuvannhan);
			
			$this->data['customers_id'] = $results['customer_id'];
			$this->data['name']         = $results['name'];
			$this->data['madn']         = $results['madn'];
			$this->data['money']        = $this->currency->format($results['price']);
			$this->data['status']       = $results['status'] ? 'Đã chuyển' : 'Chưa chuyển';
			$this->data['date_added']   = date($this->language->get('date_format_short'), strtotime($results['date_added']));
			$this->data['href']   =  $this->url->link('customer/edit');
			$this->template = 'sale/hoahong.tpl';	
			$this->children = array(
				'common/footer',
				'common/header'	
			);
							
			$this->response->setOutput($this->render());	
		}
	}
  }
}
?>